//
//  WWSearchAreaTableViewController.m
//  WhatWeather
//
//  Created by byn on 15/11/28.
//  Copyright © 2015年 byn. All rights reserved.
//

#import "WWSearchAreaTVC.h"
#import "WWSearchResultTVCell.h"
#import "WWAreadIdBridge.h"
#import "ShowDetailWeatherTVC.h"
#import "WWCollectedCitiesManage.h"
#import "WWAreaInfo.h"

@interface WWSearchAreaTableViewController ()

@end

@implementation WWSearchAreaTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.searchAreaBar.delegate = self;
    self.searchAreaBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
    
    UIImage *img = [UIImage imageNamed:@"blue_mask.png"];
    UIImageView *bgImageView = [[UIImageView alloc] initWithImage:img];
    bgImageView.frame = CGRectMake(0, 0, self.tableView.frame.size.width, self.tableView.frame.size.height);
        //[_backView addSubview:bgImageView];
    [self.tableView setBackgroundColor:[UIColor colorWithPatternImage:img]];
}

-(BOOL) searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([searchBar isEqual:_searchAreaBar]) {
        WWAreadIdBridge *areaBridge = [WWAreadIdBridge getInstance];
        NSMutableString *searchText = (NSMutableString *)[_searchAreaBar.text stringByReplacingCharactersInRange:range withString:text];
        _areaArray = [areaBridge queryDataWithSearchText:searchText searchPattern:SearchPatternLike];
        [self.tableView reloadData];
    }
    return YES;
}
- (BOOL) searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    return YES;
}
-(BOOL) searchBarShouldEndEditing:(UISearchBar *)searchBar{
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [_areaArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellidentifier = @"WWSearchAreaIdentifier";
    WWSearchResultTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier forIndexPath:indexPath];
    // Configure the cell...
    WWAreaInfo *areaInfo = [_areaArray objectAtIndex:indexPath.row];
    NSString *areaText = [NSString stringWithFormat:@"%@",areaInfo.nameCN];
    cell.areaName.text = areaText;
    return cell;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WWAreaInfo *areaInfo = [_areaArray objectAtIndex:indexPath.row];
        //[self tableView:self.tableView didDeselectRowAtIndexPath:indexPath];
    for (int i = 0; i < [self.navigationController.viewControllers count]; i++) {
        if ([self.navigationController.viewControllers[i] isKindOfClass:[ShowDetailWeatherTVC class]]) {
            ShowDetailWeatherTVC *mainViewController = [self.navigationController.viewControllers objectAtIndex:i];
            mainViewController.curAreaId = areaInfo.areaId;
            [[WWCollectedCitiesManage sharedCollectedCities] addNewCity:areaInfo.areaId cityName:areaInfo.nameCN];
            [[WWCollectedCitiesManage sharedCollectedCities] addCurSelCity:areaInfo.areaId];
            [mainViewController startReqWeatherDirectly];
            [self.navigationController popViewControllerAnimated:TRUE];
        }
    }
}

@end












































